package org.teamh.gui.createGame;

import org.teamh.artificialIntelligence.StartAI;
import org.teamh.gui.lobby.LobbyController;
import org.teamh.logic.lobby.Game;

import de.lessvoid.nifty.controls.Button;
import de.lessvoid.nifty.controls.TextField;
import de.lessvoid.nifty.elements.Element;

public class CreateGameController {

	private LobbyController lobbyController;
	private Element createGame;
	private CreateGameScreen createGameScreen;
	private String player;

	private boolean numberOfPlayersChoosed = false;
	private boolean numberOfBotsChoosed = false;

	private int bots;
	private String lastMapName;

	public boolean duplicateName(String currentName) {
		boolean noDuplicate = true;
		
		for (Game g : lobbyController.getGui().getStart().getModel().getGames()) {
			if (g.getName().equals(currentName)) {
				noDuplicate = false;
			}
		}
		
		if (currentName.contains(" ")) {
			noDuplicate = false;
		}
		
		return noDuplicate;
	}
	
	public void updateCreateButton() {
		if ((numberOfPlayersChoosed == true)
				&& (numberOfBotsChoosed == true)
				&& createGameScreen.getCreateGame()
						.findNiftyControl("eingabeName", TextField.class)
						.getText() != null
				&& createGameScreen.getCreateGame()
						.findNiftyControl("eingabeName", TextField.class)
						.getText().length() > 0
				&& duplicateName(createGameScreen.getCreateGame()
						.findNiftyControl("eingabeName", TextField.class)
						.getText())		
				) {
			createGame.findNiftyControl("createGameButtonPopUp", Button.class)
					.enable();
		} else {
			createGame.findNiftyControl("createGameButtonPopUp", Button.class)
					.disable();
		}
	}

	public CreateGameController(LobbyController lobbyController) {
		this.lobbyController = lobbyController;
		createGame = lobbyController.getCreateGameScreen().getCreateGame();
		createGameScreen = this.lobbyController.getCreateGameScreen();
	}

	public void changeVisibility(String showname, String hidename) {
		show(showname);
		hide(hidename);
	}

	public void hideAllTexfieldEffects() {
		hide("LoginNameTextfieldEffect");
		hide("LoginPasswordTextfieldEffect");
	}

	public void hide(String hidename) {
		createGame.findElementByName(hidename).hide();
	}

	public void show(String showname) {
		createGame.findElementByName(showname).show();
	}

	// Hoveraktionen
	public void showInformationtext(String name) {
		hideAllInformationtexts();
		createGame.findElementByName(name).show();
	}

	public void showInformationtextAndHovereffect(String name) {// name wird
																// �bergeben und
																// dann der
																// entsprechende
																// name des
																// informationstextes
																// und des
																// rahmens
																// zusammengebaut
																// und mit
																// diesem namen
																// dann die
																// entsprechende
																// funktion
																// aufgerufen
		hideAllInformationtexts();
		String informationtextname = "informationtext" + name;
		String Hovereffectname = "Hovereffect" + name;
		createGame.findElementByName(informationtextname).show();
		createGame.findElementByName(Hovereffectname).show();
	}

	// Ende Hoveraktionen
	public void hideAllInformationtexts() {
		createGame.findElementByName("informationtextImage2Players").hide();
		createGame.findElementByName("informationtextImage3Players").hide();
		createGame.findElementByName("informationtextImage4Players").hide();
		hideAllInformationtextsBots();
	}

	public void hideAllInformationtextsAndHovereffekts() {
		createGame.findElementByName("informationtextImage2Players").hide();
		createGame.findElementByName("informationtextImage3Players").hide();
		createGame.findElementByName("informationtextImage4Players").hide();
		createGame.findElementByName("HovereffectImage2Players").hide();
		createGame.findElementByName("HovereffectImage3Players").hide();
		createGame.findElementByName("HovereffectImage4Players").hide();
		hideAllInformationtextsAndHovereffektsBots();
	}

	public void hideAllBordersClicked() {
		createGame.findElementByName("borderImage2PlayersClicked").hide();
		createGame.findElementByName("borderImage3PlayersClicked").hide();
		createGame.findElementByName("borderImage4PlayersClicked").hide();
	}

	// Festlegen der Panelclickactionen
	// Image2Players/Image3Players/Image4Players
	public void image2Playersclick() {
		hideAllBotImages();
		hideAllBordersClicked();
		player = "2";
		createGame.findElementByName("borderImage2PlayersClicked").show();
		createGame.findElementByName("ImageNoBots").show();
		createGame.findElementByName("Image1Bots").show();
		createGame.findElementByName("Image2Bots").show();
		hideAllBordersClickedBots();
		hideAllHovereffectBotImages();
		numberOfPlayersChoosed = true;
		// numberOfBotsChoosed=false;
		if (bots > 2) {
			selectBot(2);
		} else {
			selectBot(bots);
		}
		updateCreateButton();
	}

	public void image3Playersclick() {
		hideAllBotImages();
		hideAllBordersClicked();
		player = "3";
		createGame.findElementByName("borderImage3PlayersClicked").show();
		createGame.findElementByName("ImageNoBots").show();
		createGame.findElementByName("Image1Bots").show();
		createGame.findElementByName("Image2Bots").show();
		createGame.findElementByName("Image3Bots").show();
		hideAllBordersClickedBots();
		hideAllHovereffectBotImages();
		numberOfPlayersChoosed = true;
		// numberOfBotsChoosed=false;
		if (bots > 3) {
			selectBot(3);
		} else {
			selectBot(bots);
		}
		updateCreateButton();
	}

	public void image4Playersclick() {
		hideAllBotImages();
		hideAllBordersClicked();
		player = "4";
		createGame.findElementByName("borderImage4PlayersClicked").show();
		createGame.findElementByName("ImageNoBots").show();
		createGame.findElementByName("Image1Bots").show();
		createGame.findElementByName("Image2Bots").show();
		createGame.findElementByName("Image3Bots").show();
		createGame.findElementByName("Image4Bots").show();
		hideAllBordersClickedBots();
		hideAllHovereffectBotImages();
		numberOfPlayersChoosed = true;
		// numberOfBotsChoosed=false;
		selectBot(bots);
		updateCreateButton();
	}

	public void hideAllBotImages() {
		// bots = 0;
		createGame.findElementByName("ImageNoBots").hide();
		createGame.findElementByName("Image1Bots").hide();
		createGame.findElementByName("Image2Bots").hide();
		createGame.findElementByName("Image3Bots").hide();
		createGame.findElementByName("Image4Bots").hide();
	}

	public void hideAllHovereffectBotImages() {
		createGame.findElementByName("HovereffectImageNoBots").hide();
		createGame.findElementByName("HovereffectImage1Bots").hide();
		createGame.findElementByName("HovereffectImage2Bots").hide();
		createGame.findElementByName("HovereffectImage3Bots").hide();
		createGame.findElementByName("HovereffectImage4Bots").hide();
	}

	// ENDE Festlegen der Panelclickactionen
	// Image2Players/Image3Players/Image4Players

	// mapPreviewclickMethode
	public void mapPreviewclick() {
		hideAllBordersClicked();
		createGame.findElementByName("borderMapPreviewImageClicked").show();
	}

	// ENDE mapPreviewclickMethode

	// createButtonclickMethode
	public void createGameButtonclick() {
		String name = createGameScreen.getCreateGame()
				.findNiftyControl("eingabeName", TextField.class).getText();

		if (createGameScreen.getMaps().getSelection().size() > 0
				&& name != null && player != null && !name.contains(" ")) {
			String mapname = createGameScreen.getMaps().getSelection().get(0)
					.toString();
			lastMapName = name;
			String message = ("create game" + " " + name + " " + "-players"
					+ " " + player + " " + "-map" + " " + mapname);
			lobbyController.getGui().getStart().getConnection()
					.sendMessage(message);

			// System.out.println("start with " + bots + " number of bots");
			for (int i = 0; i < bots; i++) {
				lobbyController.getGui().getStart().getConnection()
						.sendMessage("create testplayer");
			}

			closePopup();
		}
		createGame.findNiftyControl("createGameButtonPopUp", Button.class)
				.disable();
	}

	// ENDE startButtonclickMethode

	public void createBot(String name, String password, String gameName,
			boolean view) {
		if (gameName != null) {
			StartAI.create(name, password, gameName);
			if (view) {
				lobbyController.kiJoinGame(gameName);
			}
			lobbyController.getGui().getStart().getModel().setGameName(null);
			lobbyController.getGui().getStart().getModel().setView(false);
		} else {
			StartAI.create(name, password, lastMapName);
		}
	}

	public void backToLobby() {
		closePopup();
	}

	private void closePopup() {
		numberOfPlayersChoosed = false;
		numberOfBotsChoosed = false;
		updateCreateButton();
		hideAllBordersClicked();
		hideAllBotImages();
		createGameScreen.getMaps().clear();
		createGameScreen.getCreateGame()
				.findNiftyControl("eingabeName", TextField.class).setText("");
		lobbyController.getNifty().closePopup(
				createGameScreen.getCreateGame().getId());
	}

	// Festlegen der Panelclickactionen
	// ImageNoBots/Image1Bots/Image2Bots/Image3Bots/Image4Bots
	// Hoveraktionen
	public void showInformationtextBots(String name) {
		hideAllInformationtextsBots();
		createGame.findElementByName(name).show();
	}

	public void showInformationtextAndHovereffectBots(String name) {// name wird
																	// �bergeben
																	// und dann
																	// der
																	// entsprechende
																	// name des
																	// informationstextes
																	// und des
																	// rahmens
																	// zusammengebaut
																	// und mit
																	// diesem
																	// namen
																	// dann die
																	// entsprechende
																	// funktion
																	// aufgerufen
		hideAllInformationtextsBots();
		String informationtextname = "informationtext" + name;
		String Hovereffectname = "Hovereffect" + name;
		createGame.findElementByName(informationtextname).show();
		createGame.findElementByName(Hovereffectname).show();
	}

	// Ende Hoveraktionen
	public void hideAllInformationtextsBots() {
		createGame.findElementByName("informationtextImageNoBots").hide();
		createGame.findElementByName("informationtextImage1Bots").hide();
		createGame.findElementByName("informationtextImage2Bots").hide();
		createGame.findElementByName("informationtextImage3Bots").hide();
		createGame.findElementByName("informationtextImage4Bots").hide();
	}

	public void hideAllInformationtextsAndHovereffektsBots() {
		createGame.findElementByName("informationtextImageNoBots").hide();
		createGame.findElementByName("informationtextImage1Bots").hide();
		createGame.findElementByName("informationtextImage2Bots").hide();
		createGame.findElementByName("informationtextImage3Bots").hide();
		createGame.findElementByName("informationtextImage4Bots").hide();
		createGame.findElementByName("HovereffectImageNoBots").hide();
		createGame.findElementByName("HovereffectImage1Bots").hide();
		createGame.findElementByName("HovereffectImage2Bots").hide();
		createGame.findElementByName("HovereffectImage3Bots").hide();
		createGame.findElementByName("HovereffectImage4Bots").hide();
	}

	public void hideAllBordersClickedBots() {
		numberOfBotsChoosed = false;
		createGame.findElementByName("borderImageNoBotsClicked").hide();
		createGame.findElementByName("borderImage1BotsClicked").hide();
		createGame.findElementByName("borderImage2BotsClicked").hide();
		createGame.findElementByName("borderImage3BotsClicked").hide();
		createGame.findElementByName("borderImage4BotsClicked").hide();
	}

	// Festlegen der Panelclickactionen
	// ImageNoBots/Image1Bots/Image2Bots/Image3Bots/Image4Bots
	public void imageNoBotsclick() {
		bots = 0;
		hideAllBordersClickedBots();
		createGame.findElementByName("borderImageNoBotsClicked").show();
		numberOfBotsChoosed = true;
		updateCreateButton();
	}

	public void image1Botsclick() {
		bots = 1;
		hideAllBordersClickedBots();
		createGame.findElementByName("borderImage1BotsClicked").show();
		numberOfBotsChoosed = true;
		updateCreateButton();
	}

	public void image2Botsclick() {
		bots = 2;
		hideAllBordersClickedBots();
		createGame.findElementByName("borderImage2BotsClicked").show();
		numberOfBotsChoosed = true;
		updateCreateButton();
	}

	public void image3Botsclick() {
		bots = 3;
		hideAllBordersClickedBots();
		createGame.findElementByName("borderImage3BotsClicked").show();
		numberOfBotsChoosed = true;
		updateCreateButton();
	}

	public void image4Botsclick() {
		bots = 4;
		hideAllBordersClickedBots();
		createGame.findElementByName("borderImage4BotsClicked").show();
		numberOfBotsChoosed = true;
		updateCreateButton();
	}

	private void selectBot(int i) {
		switch (i) {
		case 0:
			imageNoBotsclick();
			break;
		case 1:
			image1Botsclick();
			break;
		case 2:
			image2Botsclick();
			break;
		case 3:
			image3Botsclick();
			break;
		case 4:
			image4Botsclick();
			break;
		}
	}

	// ENDE Festlegen der Panelclickactionen
	// ImageNoBots/Image1Bots/Image2Bots/Image3Bots/Image4Bots

	public CreateGameScreen getCreateGameScreen() {
		return createGameScreen;
	}
}
